import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AbstractModule } from './modules/abstract/abstract.module';
import { CategoriesModule } from './modules/categories/categories.module';
import { TagsModule } from './modules/tags/tags.module';
import configuration from './config/configuration';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { TransformInterceptor } from './common/interceptor/transform.interceptor';
import { PublicConfigModule } from './modules/config/config.module';
import { AdminUserModule } from './modulesAdmin/user/user.module';
import { VisitLogModule } from './modules/visit_log/log.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      // load: [configuration],
      // envFilePath: ['.env.prod', '.env'],
      envFilePath: process.env.NODE_ENV === 'production' ? '.env.prod' : '.env',

    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('DATABASE_HOST'),
        port: parseInt(configService.get('DATABASE_PORT'), 10),
        username: configService.get('DATABASE_USER'),
        password: configService.get('DATABASE_PASSWD'),
        database: configService.get('DATABASE_DATABASE'),
        entities: [__dirname + '/**/*.entity{.ts,.js}'], // 扫描本项目中.entity.ts或者.entity.js的文件 // 在此处指定实体，或使用autoLoadEntities: true自动加载
        synchronize: true, // configService.get('DATABASE_SYNCHRONIZE') !== 'false', // 定义数据库表结构与实体类字段同步(这里一旦数据库少了字段就会自动加入,根据需要来使用) // !注意：在生产环境中应设置为false
        // envFilePath: ['.env.prod', '.env'],
      }),
    }),
    AbstractModule,
    CategoriesModule,
    TagsModule,
    PublicConfigModule,
    VisitLogModule,

    AdminUserModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor
    }
  ],
})
export class AppModule {}
